package com.javasm.controller;

import com.alibaba.fastjson.JSON;
import com.javasm.entity.*;
import com.javasm.service.impl.BookServiceImpl;
import com.javasm.service.impl.GameServiceImpl;
import com.javasm.service.impl.MenuServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author WangJie
 * @version 1.0
 * @Description:
 * @date: 2020/7/20 10:48
 * @since JDK 1.8
 */
@WebServlet("/book")
public class BookServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("method");
        PrintWriter pw = resp.getWriter();
        Map<String, Object> returnData = new HashMap<>();
        PageInfo pageInfo = null;
        List<AdminMenu> adminMenus = getMenu(req, resp);
        if ("getBookList".equals(method)) {
            pageInfo = getBookList(req, resp);
            List<BookTopSort> topSortList = new BookServiceImpl().selectBookTopSort(); // 获得所有一级分类名称
            returnData.put("topSortList", topSortList);
        } else if ("getSecondSort".equals(method)) { // 根据一级分类的id获得所有二级分类，一级分类的下拉框change事件触发
            List<BookSecondSort> secondSortList = getSecondSort(req, resp);
            returnData.put("secondSortList", secondSortList);
            pw.print(JSON.toJSONString(returnData));
            pw.flush();
            pw.close();
            return;

        } else if ("getBookSortInfo".equals(method)){ // 得到图书分类信息
            pageInfo = getBookSortInfo(req,resp);
            List<BookTopSort> topSortList = new BookServiceImpl().selectBookTopSort(); // 获得所有一级分类名称
            returnData.put("topSortList", topSortList);
        }
        AdminUser loginAccount = (AdminUser) req.getSession().getAttribute("loginAccount");
        // 判断查询条件
        if (pageInfo == null) {
            returnData.put("returnCode", ReturnCode.ERROR.getCode());
            returnData.put("returnMsg", ReturnCode.ERROR.getMsg());
        } else {
            returnData.put("returnCode", ReturnCode.SUCCESS.getCode());
            returnData.put("returnAccount", loginAccount);
            returnData.put("returnData", adminMenus);
            returnData.put("pageInfo", pageInfo);
        }
        pw.print(JSON.toJSONString(returnData));
        pw.flush();
        pw.close();
    }

    // 分页查询所有一级、二级图书分类信息
    private PageInfo getBookSortInfo(HttpServletRequest req, HttpServletResponse resp) {
        Integer page = Integer.parseInt(req.getParameter("page"));
        Integer pageSize = Integer.parseInt(req.getParameter("pageSize"));
        return new BookServiceImpl().selectBookSortByPage(page,pageSize);
    }


    // 根据一级分类id获得二级分类
    private List<BookSecondSort> getSecondSort(HttpServletRequest req, HttpServletResponse resp) {
        String tsbId = req.getParameter("tsbId");
        return new BookServiceImpl().selectBookSecondSortByTsbId(tsbId);
    }

    // 条件查询图书信息
    private PageInfo getBookList(HttpServletRequest req, HttpServletResponse resp) {
        Integer page = Integer.parseInt(req.getParameter("page"));
        Integer pageSize = Integer.parseInt(req.getParameter("pageSize"));
        Book book = new Book();
        String bid = req.getParameter("bid");
        if (bid != null && !"".equals(bid.trim())) {
            book.setBid(Integer.parseInt(bid));
        }
        String bookName = req.getParameter("bookName");
        String tsbId = req.getParameter("tsbId");
        String ssbId = req.getParameter("ssbId");
        book.setBook_name(bookName);
        book.setTsb_id(Integer.parseInt(tsbId));
        book.setSsb_id(Integer.parseInt(ssbId));
        return new BookServiceImpl().selectBookByPage(page, pageSize, book);
    }

    // 根据登录的账户信息侧边栏展示可操作的菜单列表
    private List<AdminMenu> getMenu(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        AdminUser adminUser = (AdminUser) req.getSession().getAttribute("loginAccount");
        return new MenuServiceImpl().selectList(adminUser.getMenu_id());
    }
}
